/**
 * Created by Syaoran on 22/01/05.
 */
import router from "@/router";
const Key = "ITG-LS";

class LocalStorage {
  constructor () {
    this.data = {}; // 所有ls数据
    this.getAllLocalStorage();
  }

  /* 获取所有localstorage方法 */
  getAllLocalStorage () {
    this.data = JSON.parse(localStorage.getItem(Key)) || {};
  }

  /**
     * 获取所有localstorage方法
     */
  getLocalStorage (name) {
    if (!name) return;
    const key = router.currentRoute.name;
    if (!this.data[key]) return undefined;
    else return this.data[key][name];
  }

  /**
     * 存入localstorage方法
     * @param {String} name: 存入信息的键值
     * @param {...} obj: 存入的信息
     */
  setLocalStorage (name, obj) {
    if (!name) return;
    const key = router.currentRoute.name;
    if (!this.data[key]) this.data[key] = {};
    this.data[key][name] = obj;
    this.update();
  }

  /* 更新localstorage */
  update () {
    localStorage.setItem(Key, JSON.stringify(this.data));
  }
}

export default LocalStorage;
